
package code.codeworld.record.browse_record.service.impl;


import code.codeworld.record.browse_record.model.BrowseRecord;
import code.codeworld.record.browse_record.model.vo.BrowseRecordVo;
import code.codeworld.common.kits.PageUtil;
import code.codeworld.common.kits.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import code.codeworld.record.browse_record.service.BrowseRecordService;
import code.codeworld.record.browse_record.mapper.BrowseRecordJPA;
import code.codeworld.record.browse_record.mapper.BrowseRecordMapper;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
 * BrowseRecordService接口实现类
 *
 * @author freecode
 * @date 2023/03/23 00:20
 **/
@Service
public class BrowseRecordServiceImpl extends ServiceImpl<BrowseRecordMapper, BrowseRecord> implements BrowseRecordService {
    @Resource
    private BrowseRecordJPA BrowseRecordJPA;

    @Override
    public Page<BrowseRecord> page(BrowseRecordVo browseRecordVo) {
        Integer[] pageNums = new Integer[]{browseRecordVo.getPageNum(), browseRecordVo.getPageSize()};
        PageUtil.getPageNums(pageNums);
        browseRecordVo.setPageable(PageRequest.of(pageNums[0], pageNums[1], Sort.Direction.DESC, "createTime"));
        BrowseRecord browseRecord = new BrowseRecord();
        browseRecord.setKeyword(browseRecordVo.getKeyword());
        browseRecord.setType(browseRecordVo.getType());
        Page<BrowseRecord> page = BrowseRecordJPA.findAll(Example.of(browseRecord), browseRecordVo.getPageable());
        if (!page.isEmpty()) {
            int autoNum = pageNums[0] * pageNums[1];
            for (BrowseRecord t : page.getContent()) {
                t.setAutoNum(++autoNum);
            }
        }
        return page;
    }

    @Override
    public Result<String> add(BrowseRecord browseRecord) {
        List<BrowseRecord> list = this.list(browseRecord);
        Date now = new Date();
        if (list.size() == 0) {
            browseRecord.setUpdateIp("");
            browseRecord.setUpdateIpAddress("");
            browseRecord.setCreateTime(now);
            try {
                BrowseRecordJPA.save(browseRecord);
                return Result.ok();
            } catch (Exception e) {
                return Result.exception(e.getMessage());
            }
        } else if (list.size() == 1) {
            browseRecord.setId(list.get(0).getId());
            browseRecord.setCreateIp(list.get(0).getCreateIp());
            browseRecord.setCreateIpAddress(list.get(0).getCreateIpAddress());
            browseRecord.setCreateTime(list.get(0).getCreateTime());
            browseRecord.setUpdateTime(now);
            return this.edit(browseRecord);
        } else {
            return Result.exception();
        }

    }

    @Override
    public Result<String> edit(BrowseRecord browseRecord) {
        BrowseRecord browseRecordDb = this.getEntityById(browseRecord.getId());
        if (ObjectUtils.isEmpty(browseRecordDb)) {
            return Result.exception();
        }
        try {
            browseRecord.setCreateTime(browseRecordDb.getCreateTime());
            BrowseRecordJPA.save(browseRecord);
            return Result.ok();
        } catch (Exception e) {
            return Result.exception(e.getMessage());
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<BrowseRecord> browseRecordList) {

        // 执行删除
    }

    @Override
    public BrowseRecord detail(BrowseRecord browseRecord) {
        return this.getEntityById(browseRecord.getId());
    }

    @Override
    public BrowseRecord getEntityById(Integer id) {
        return BrowseRecordJPA.findById(id).get();
    }

    public List<BrowseRecord> list(BrowseRecord browseRecord) {
        LambdaQueryWrapper<BrowseRecord> queryWrapper = new LambdaQueryWrapper<BrowseRecord>();
        queryWrapper.eq(BrowseRecord::getCreateIp, browseRecord.getCreateIp());
        queryWrapper.eq(!ObjectUtils.isEmpty(browseRecord.getHref()), BrowseRecord::getHref, browseRecord.getHref());
        queryWrapper.eq(!ObjectUtils.isEmpty(browseRecord.getKeyword()), BrowseRecord::getKeyword, browseRecord.getKeyword());
        return super.list(queryWrapper);
    }

}
